package com.jhgsys.internal.common.utils;

import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间工具类
 *
 * @author MrBird
 */
public class DateUtil {

    public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";

    public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String CST_TIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy";
    public static final String YYYY_MM_DD_PATTERN = "yyyy-MM-dd";
    public static final String START_DATA_PATTERN = " 00:00:00";
    public static final String END_DATA_PATTERN = " 23:59:59";

    public static String formatFullTime(LocalDateTime localDateTime) {
        return formatFullTime(localDateTime, FULL_TIME_PATTERN);
    }

    public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static String getDateFormat(Date date, String dateFormatType) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatType, Locale.CHINA);
        return simpleDateFormat.format(date);
    }

    public static String formatCstTime(String date, String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(CST_TIME_PATTERN, Locale.US);
        Date usDate = simpleDateFormat.parse(date);
        return DateUtil.getDateFormat(usDate, format);
    }

    public static String formatInstant(Instant instant, String format) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    public static Date getCurrentDay() {
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = LocalDate.now();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static Date getPastDate(Date date,int pastDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH,pastDay);
        return calendar.getTime();
    }

    /**
     * 获取当年元旦
     * @return
     */
    public static Date getCurrentYuanDan() {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.DAY_OF_YEAR,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 获取某一年元旦
     * @return
     */
    public static Date getYuanDan(String year) {
        String date = year+"-01-01 00:00:00";
        return parseDate(date,FULL_TIME_SPLIT_PATTERN);
    }

    /**
     * 获取某一年最后一天
     * @return
     */
    public static Date getYearLastTime(String year) {
        String date = year+"-12-31 23:59:59";
        return parseDate(date,FULL_TIME_SPLIT_PATTERN);
    }

    public static Date parseDate(String dateStr, String patt) {
        try {
            return DateUtils.parseDate(dateStr, patt);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static int compareWithNow(Date date){
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_PATTERN);
        int ret = 0;
        try {
            Date now = new Date();
            String nowStr = format.format(now);
            String dateStr = format.format(date);
            Date now1 = format.parse(nowStr);
            Date date1 = format.parse(dateStr);
            int result = now1.compareTo(date1);
            if(result<=0){
                ret = 1;
            }else{
                ret = 0;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return ret;
    }

}
